package com.example.diagnose.Controller;

import cn.hutool.core.util.IdUtil;
import com.example.diagnose.Config.CommonResponse;
import com.example.diagnose.Domain.User;
import com.example.diagnose.Dto.Login;
import com.example.diagnose.Dto.Msg;
import com.example.diagnose.Service.SendSmsService;
import com.example.diagnose.Service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/user")
@Tag(name = "用户模块")
public class UserController {
//唯一id用例
//String userId = IdUtil.simpleUUID();

    @Resource
    UserService userService;

    @Resource
    SendSmsService sendSmsService;

    @SneakyThrows
    @Operation(summary = "发送手机验证码")
    @GetMapping("/{tel}")
    public CommonResponse<Msg> sendMsg(@PathVariable("tel") String tel) {
        if(userService.getUserByTel(tel) == null){
            String code = sendSmsService.getCode();
            sendSmsService.sendMessage(tel, code);
            Msg msg = new Msg();
            msg.setCode(code);
            msg.setTel(tel);
            return  CommonResponse.BackForSuccess("已发送验证码", msg);
        }else {
            return CommonResponse.BackForError("该手机号已存在账号");
        }

    }

    @Operation(summary = "新增用户")
    @PostMapping
    public CommonResponse<User> addUser(@RequestBody User user) {
        String account = IdUtil.simpleUUID();
        user.setAccount(account);
        if (user.getPwd() == null) {
            return CommonResponse.BackForError("密码为空，新增用户失败");
        } else {
            int result = userService.addUser(user);
            if (result == 0) {
                return CommonResponse.BackForError("新增用户失败");
            } else {
                return CommonResponse.BackForSuccess("新增用户成功", "http://127.0.0.1:8081/user");
            }
        }
    }

    @Operation(summary = "修改用户信息")
    @PutMapping
    public CommonResponse<User> updateUser(@RequestBody User user) {
        if (user.getAccount() == null) {
            return CommonResponse.BackForError("账号为空，修改用户信息失败");
        } else if (user.getPwd() == null) {
            return CommonResponse.BackForError("密码为空，修改用户信息失败");
        } else {
            int result = userService.updateUser(user);
            if (result == 0) {
                return CommonResponse.BackForError("修改用户信息失败");
            } else {
                return CommonResponse.BackForSuccess("修改用户信息成功", "http://127.0.0.1:8081/user");
            }
        }
    }

    @Operation(summary = "获取用户信息")
    @GetMapping
    public CommonResponse<User> getUser(@RequestParam("account") String account) {
        User user = userService.getUserByAccount(account);
        if (user == null) {
            return CommonResponse.BackForError("未搜索到相关用户信息");
        }else {
            return CommonResponse.BackForSuccess("搜索用户信息成功", user, "http://127.0.0.1:8081/user");
        }
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public CommonResponse<User> signOnMsg(@RequestBody Login login) {
        boolean flag = false;
        User user = new User();
        if(login.getAccount() != null){
            user = userService.getUserByAccount(login.getAccount());
            if(Objects.equals(user.getPwd(), login.getPwd())){
                flag = true;
            }
        }else if(login.getTel() != null){
            user = userService.getUserByTel(login.getTel());
            if(Objects.equals(user.getPwd(), login.getPwd())){
                flag = true;
            }
        }else {
            return CommonResponse.BackForError("该账户不存在");
        }

        if(!flag){
            return CommonResponse.BackForError("密码错误");
        }else {
            return CommonResponse.BackForSuccess("登录成功", user);
        }
    }

}
